﻿using Camc.Quality.Configuration;
using Microsoft.Extensions.Configuration;
using System;
using System.IO;

namespace Camc.Quality.Quality
{
    public static class ConfigHelper
    {
        private static IConfigurationRoot _appConfiguration = AppConfigurations.Get(System.Environment.CurrentDirectory);

        //用法1(有嵌套)：GetAppSetting("Authentication", "JwtBearer:SecurityKey")
        //用法2：GetAppSetting("App", "ServerRootAddress")
        public static string GetAppSetting(string section, string key)
        {
            return _appConfiguration.GetSection(section)[key];
        }

        public static string GetConnectionString(string key)
        {
            return _appConfiguration.GetConnectionString(key);
        }

        /// <summary>
        /// 日志
        /// </summary>
        /// <param name="message"></param>
        public static void CreateLogText(string message)
        {
            string path = "";
            DateTime now = DateTime.Now;
            string Path_txt = "";
            try
            {
                path = Environment.CurrentDirectory + "\\" + "Log";
                if (Directory.Exists(path) == false)
                {
                    Directory.CreateDirectory(path);
                }
                string path_d = path + "\\" + now.ToString("yyyyMMdd");

                if (Directory.Exists(path_d) == false)
                {
                    Directory.CreateDirectory(path_d);
                }
                Path_txt = path_d + "\\" + "UploadChunk_log.txt";
                if (Directory.Exists(Path_txt) == false)
                {
                    Directory.CreateDirectory(path_d);
                }
            }
            catch (Exception e)
            {
            }

            try
            {
                StreamWriter FileWriter = new StreamWriter(Path_txt, true);
                try
                {
                    FileWriter.WriteLine("[" + now.ToString("yyyy-MM-dd HH:mm:ss") + "]" + message);
                    FileWriter.Flush();
                }
                catch (Exception ex)
                {

                    return;
                }
                finally
                {
                    FileWriter.Close();
                }
            }
            catch (Exception ex)
            {
            }
        }



    }
}
